package com.yitao.cms.service.shopInfo;

import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.shopInfo.HotSearchDto;
import com.yitao.client.model.shopInfo.HotSearchModel;
import com.yitao.client.model.shopInfo.QHotSearchModel;
import com.yitao.cms.utils.BaseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class HotSearchCmsService extends GenericEntityService<HotSearchModel, HotSearchDto, QHotSearchModel> {

	private BaseUtils baseUtils = BaseUtils.getInstance();

	public HotSearchCmsService(EntityManager entityManager) {
		super(entityManager);
	}

	/**
	 * 保存热搜词
	 * @param request
	 * @param dto
	 * @return
	 */
	@Transactional
	public DataOutput saveHotSearchInfo(HttpServletRequest request, HotSearchDto dto){


		QHotSearchModel hotSearchModel = QHotSearchModel.hotSearchModel;
		List<HotSearchModel> list =
				queryFactory.select(hotSearchModel).from(Q).where(Q.isDelete.
						eq(0).and(Q.searchKey.eq(dto.getSearchKey()))).fetch();

		HotSearchModel model = new HotSearchModel();
		BeanUtils.copyProperties(dto, model);
		model.setIsDelete(0);
		model.setCreateTime(new Date());
		HotSearchModel save = repository.save(model);
		if (save == null) {
			return new DataOutput(CommonStatus.SERVER_ERROR.getCode(), CommonStatus.SERVER_ERROR.getMessage());
		}
		return new DataOutput(CommonStatus.SUCCESS);
	}


}
